//
//  DebugMainViewController.m
//  YK_DebugManager
//
//  Created by edward on 2021/1/12.
//

#import "DebugMainViewController.h"
#import "DebugItemManager.h"
#import "DebugNetWorkManager.h"
#import "DebugMainViewCell.h"
#import "DebugMainSwitchCell.h"
#import "DebugMainViewLayout.h"
#import "DebugKeyValue.h"



@interface DebugMainViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
///
@property(nonatomic,strong) UICollectionView *collectionView;

@end

@implementation DebugMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self bindData];
}

- (void)setupUI
{
    self.title = @"主页面";
    [self.view addSubview:self.collectionView];
    [self setNeedsStatusBarAppearanceUpdate];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification:) name:@"reloadMainVC" object:nil];
    
}
- (void)bindData
{
    
}
- (void)getNotification:(NSNotification *)notification
{
    NSString *type = notification.name;
    if ([type isEqualToString:@"reloadMainVC"]){
        [self.collectionView reloadData];
    }
}

#pragma mark -----------collectionView------------

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return [DebugItemManager sharedInstance].customItems.count > 0 ? 3:2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return [DebugItemManager sharedInstance].switchItems.count;
            break;
        case 1:
            return [DebugItemManager sharedInstance].mainItems.count;
            break;
        case 2:
            return [DebugItemManager sharedInstance].customItems.count;
            break;
        default:
            return 0;
            break;
    }
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section > 0) {
        DebugMainViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(DebugMainViewCell.class) forIndexPath:indexPath];
        [cell setData:indexPath];
        return cell;
    }else
    {
        DebugMainSwitchCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(DebugMainSwitchCell.class) forIndexPath:indexPath];
        [cell setData:indexPath];
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakself = self;
    [[DebugItemManager sharedInstance] getItemDetail:indexPath detailBlock:^(UIImage * _Nonnull itemImage, NSString * _Nonnull itemTitle, void (^ _Nonnull itemClickBlock)(void (^ _Nonnull)(UIViewController * _Nonnull, NSInteger, BOOL), void (^ _Nonnull)(void))) {
        __strong typeof(weakself) strongself = weakself;
        
       __block void(^ _Nonnull callBack)(UIViewController *vc, NSInteger type, BOOL animated) = ^(UIViewController *vc, NSInteger type, BOOL animated){
           if (type == 0) {
               [strongself.navigationController pushViewController:vc animated:animated];
           }else if (type == 1){
               [strongself presentViewController:vc animated:animated completion:nil];
           }else{
               return;
           }
        };
        
        __block void(^dissMissMain)(void) = ^{
            if (strongself.showOrDissMiss) {
                strongself.showOrDissMiss();
            }
        };
        
        if (itemClickBlock) {
            itemClickBlock(callBack,dissMissMain);
        }
        
    }];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass(UICollectionReusableView.class) forIndexPath:indexPath];
    
    view.backgroundColor = [UIColor lightGrayColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, view.bounds.size.height)];
    titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
    NSString *title = @"";
    switch (indexPath.section) {
        case 0:{
            title = @"功能开关";
        }break;
        case 1:{
            title = @"基本功能";
        }break;
        case 2:{
            title = @"自定义功能";
        }break;
        default:
            break;
    }
    titleLabel.text = title;
    if (@available(iOS 13.0, *)) {
        titleLabel.textColor = UIColor.labelColor;
    } else {
        titleLabel.textColor = UIColor.blackColor;
    }
    [view addSubview:titleLabel];
    return view;
}

//header的宽高
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(debug_screenW, 25);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (UICollectionView *)collectionView
{
    if(!_collectionView)
    {
        DebugMainViewLayout *layout = [[DebugMainViewLayout alloc]init];
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, debug_top_Height, self.view.bounds.size.width, self.view.bounds.size.height - debug_top_Height - (self.view.safeAreaInsets.bottom)) collectionViewLayout:layout];
        collectionView.backgroundColor = UIColor.clearColor;
        collectionView.layer.borderColor = UIColor.clearColor.CGColor;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.scrollEnabled = YES;
        collectionView.delegate = self;
        collectionView.dataSource = self;
        [collectionView registerClass:DebugMainViewCell.class forCellWithReuseIdentifier:NSStringFromClass(DebugMainViewCell.class)];
        [collectionView registerClass:UICollectionReusableView.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass(UICollectionReusableView.class)];
        [collectionView registerClass:DebugMainSwitchCell.class forCellWithReuseIdentifier:NSStringFromClass(DebugMainSwitchCell.class)];
        _collectionView = collectionView;
    }
    return _collectionView;
}

- (void)setView:(UIView *)view
{
    [super setView:view];
}

- (void)setTitle:(NSString *)title
{
    [super setTitle:@"主页面"];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
